import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  // 登录路由
  {
    path: '/',
    name: 'Head',
    component: () => import('../views/Head/index.vue'),
    redirect:'/title',
    meta:{
      id:1
    },
    children: [
      // 首页
      {
        path: '/title',
        name: 'Title',
        component: () => import('../views/Title/index.vue'),
      },
      // 用户日志
      {
        path:'/log',
        name:'Log',
        component:() => import('../views/lug/index')
      },
      // 题库页
      {
        path: '/home',
        name: 'Home',
        component: () => import('../views/Home/index.vue')
      },     
      // 课程页
      {
        path: '/qtype',
        name: 'qtype',
        component: () => import('../views/Home/components/qtype.vue')
      },
      // 难度页
      {
        path: '/nandugu',
        name: 'nandugu',
        component: () => import('../views/Home/components/nandugu.vue')
      },
      // 角色管理页
      {
        path: '/Userpush',
        name: 'Userpush',
        component: () => import('../views/Userpush/index.vue')
      },
      // 当先用户资料页
      {
        path: '/management',
        name: 'usermanagement',
        component: () => import('../views/usermanagement/index.vue')
      },
      // 用户管理页
      {
        path: '/UserorRole',
        name: 'UserorRole',
        component: () => import('../views/UserorRole/index.vue')
      },
      // 选择路由
      {
        path: '/ItemManagement',
        name: 'ItemManagement',
        redirect:'/ItemManagement/danxuan',
        component: () => import('../views/ItemManagement/index.vue'),
        children:[
          // 单选页
          {
            path:'/ItemManagement/danxuan',
            name: 'danxuan',
            component: () => import('../views/ItemManagement/components/checkbox.vue'),
          },
          // 多选页
          {
            path:'/ItemManagement/Multiplechoice',
            name: 'Multiplechoice',
            component: () => import('../views/ItemManagement/components/Multiplechoice.vue'),
          },
          // 判断
          {
            path:'/ItemManagement/judge',
            name: 'judge',
            component: () => import('../views/ItemManagement/components/judge.vue'),
          },
          // 填空
          {
            path:'/ItemManagement/fillInTheBlanks',
            name: 'fillInTheBlanks',
            component: () => import('../views/ItemManagement/components/fillInTheBlanks.vue'),
          },
          // 简答
          {
            path:'/ItemManagement/jianda',
            name: 'jianda',
            component: () => import('../views/ItemManagement/components/jianda.vue'),
          },
        ]
      },
    ]
  },
  // 登录页
  {
    path: '/Login',
    name: 'Login',
    component: () => import('../views/Login/index.vue'),
  },
  //修改
  {
    path:'/upMultiplechoice',
    name:'upMultiplechoice',
    component:() => import('../views/Home/components/upcheckbox'),
  },
  {
    path:'/upcheckbox',
    name:'upcheckbox',
    component:() => import('../views/Home/components/upMultiplechoice'),
  },
  {
    path:'/upfillInTheBlanks',
    name:'upfillInTheBlanks',
    component:() => import('../views/Home/components/upfillInTheBlanks'),
  },
  {
    path:'/upjudge',
    name:'upjudge',
    component:() => import('../views/Home/components/upjudge'),
  },
  //查看页
  {
    path:'/ckakan/getpcheckbox',
    name:'getpcheckbox',
    component:() => import('../views/Home/components/chakan/checkbox'),
  },
]


//   公共路由 先挂载
const listpath = [
  {
    path: '/Login',
    name: 'Login',
    component: () => import('../views/Login/index.vue'),
  },
  {
    path: '/',
    name: 'Head',
    component: () => import('../views/Head/index.vue'),
    redirect:'/title',
    children:[
      // 首页
      {
        path: '/title',
        name: 'Title',
        component: () => import('../views/Title/index.vue'),
      },
      {
        path: '/management',
        name: 'usermanagement',
        component: () => import('../views/usermanagement/index.vue')
      },
      {
        path: '/ItemManagement',
        name: 'ItemManagement',
        redirect:'/ItemManagement/danxuan',
        component: () => import('../views/ItemManagement/index.vue'),
        children:[
          // 单选页
          {
            path:'/ItemManagement/danxuan',
            name: 'danxuan',
            component: () => import('../views/ItemManagement/components/checkbox.vue'),
          },
          // 多选页
          {
            path:'/ItemManagement/Multiplechoice',
            name: 'Multiplechoice',
            component: () => import('../views/ItemManagement/components/Multiplechoice.vue'),
          },
          // 判断
          {
            path:'/ItemManagement/judge',
            name: 'judge',
            component: () => import('../views/ItemManagement/components/judge.vue'),
          },
          // 填空
          {
            path:'/ItemManagement/fillInTheBlanks',
            name: 'fillInTheBlanks',
            component: () => import('../views/ItemManagement/components/fillInTheBlanks.vue'),
          },
          // 简答
          {
            path:'/ItemManagement/jianda',
            name: 'jianda',
            component: () => import('../views/ItemManagement/components/jianda.vue'),
          },
        ]
      },
    ]
  },
  //修改
  {
    path:'/upMultiplechoice',
    name:'upMultiplechoice',
    component:() => import('../views/Home/components/upcheckbox'),
  },
  {
    path:'/upcheckbox',
    name:'upcheckbox',
    component:() => import('../views/Home/components/upMultiplechoice'),
  },
  {
    path:'/upfillInTheBlanks',
    name:'upfillInTheBlanks',
    component:() => import('../views/Home/components/upfillInTheBlanks'),
  },
  {
    path:'/upjudge',
    name:'upjudge',
    component:() => import('../views/Home/components/upjudge'),
  },
  {
    path:'/upjianda',
    name:'upjianda',
    component:() => import('../views/Home/components/upjianda'),
  },
  //查看页
  {
    path:'/ckakan/getpcheckbox',
    name:'getpcheckbox',
    component:() => import('../views/Home/components/chakan/checkbox'),
  },
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes:listpath,
})

//  引用axios方法
import axios from '../views/api/login'
// 获取当前用户
axios.danglogins().then(function(res){
  let data = res.data.user.userId
  // console.log(data)
  // 获取当前用户的所有权限
  axios.getyonghu(data).then(function(res){
    // 存储
    let commonUserRouter = res.data.list
    // 创建一个新数组 存放 路由的 name
    let arr = []
    for(var i=0;i<commonUserRouter.length;i++){
      for(var x=0;x<commonUserRouter[i].list.length;x++){
        arr.push(commonUserRouter[i].list[x].cname)
      }
    }
    // console.log(arr)
    // 在全部路由中提取该用户的权限路由，过滤
    let paths = routes.filter(function(page){
      //  判断每条路由里是否有子路由
      if(page.children == undefined){
        // 没有直接返回
        return arr.includes(page.name)
      }else{
        // 有的话创建一个path 在子路由里进行过滤
        let path = page.children.filter(function(pages){
          return arr.includes(pages.name)
        })
        // console.log(path)
        // 循环过滤出来的内容逐条挂载
          for(var j=0;j<path.length;j++){
            //  重新创建 vuerouter
            // console.log(path[j])
            //  添加路由
            router.addRoute('Head',path[j])
            // router.addRoute(path[j])
          }
      }
    })
    for(var g=0;g<paths.length;g++){
      router.addRoute(paths[g])
    }
  })
})

// 思路

// 先挂载公共路由 ， 获取当前用户 ， 查询当前用户的所有路由 ， 重新挂在 当前用户所有路由


router.beforeEach((to,from,next) => {
  if (to.path != '/Login') {
    if (localStorage.getItem('token')) {
      next();
    } else {
      next('/Login');
    }
  } else {
    next();
  }
})

export default router
